package com.android.widget.radio;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.LinearLayout;

import com.android.widget.demo.R;

public class RadioGroupActivity extends Activity implements RadioGroup.OnCheckedChangeListener {

    private TextView mTxtChoice;
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_radio_group);
        mRadioGroup = (RadioGroup) findViewById(R.id.sex);

	/*
	//show you how to create new RadioButton with java code.
        RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setId(R.id.snack);
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 0, layoutParams);
	*/
        mRadioGroup.setOnCheckedChangeListener(this);
        mTxtChoice = (TextView) findViewById(R.id.choice);
        RadioButton button = (RadioButton)findViewById(mRadioGroup.getCheckedRadioButtonId());
        String text = button.getText().toString();
        mTxtChoice.setText("Your choice: " +  text);
	//show you how to set checked RadioButton with java code.
	mRadioGroup.check(R.id.female);
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
	RadioButton button = (RadioButton)findViewById(checkedId);
        String text = button.getText().toString();
        mTxtChoice.setText("Your choice: " + text);
    }
}
